package edu.ucla.cs.cs144;

import java.io.*;
import java.text.*;
import java.util.*;

import java.io.IOException;
import java.io.StringReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;

import edu.ucla.cs.cs144.Tag;
import edu.ucla.cs.cs144.PayNow;

public class PayNowServlet extends HttpServlet implements Servlet {
       
    public PayNowServlet() {}

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        HttpSession session = request.getSession(true);		// Don't know what to do with this yet
        
        PayNow paynow = (PayNow)session.getAttribute("paynow");
        
		JSONObject json = new JSONObject();
		json.put("ItemID", paynow.ItemID);
		json.put("ItemName", paynow.ItemName);
		json.put("BuyPrice", paynow.BuyPrice);
		json.put("CreditCard", paynow.CreditCard);
		json.put("ServerName",request.getServerName());
		json.put("ContextPath",request.getContextPath());
		
		request.setAttribute("result", json.toString());
		request.getRequestDispatcher("/payNowResult.jsp").forward(request, response);
    }

	// Encodes a string with escape characters
	private String encode(String s) {
		
		return s.replaceAll("\"", "&quot;")
				.replaceAll("\'", "&apos;")
				.replaceAll("&", "&amp;")
				.replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
}
